package com.fram.controller.sys;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.entity.ProjectCategory;
import com.fram.service.admin.CategroyService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月29日 - 下午3:00:13 @description：->
 */
@RestController
@RequestMapping("/sys/{version}/auth/category")
public class CategoryController {

	private static Logger LOG = LoggerFactory.getLogger(CategoryController.class);

	@Resource(name = "categroyService")
	private CategroyService categroyService;

	/** 系统类型列表 */
	@ApiVersion(1.0)
	@RequestMapping("list")
	public ResponseMsg list(Integer level, Long parentId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(categroyService.getProjectCategroy(level, parentId));
		} catch (ProException e) {
			LOG.error("-系统-系统类型列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-系统类型列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 详情 **/
	@ApiVersion(1.0)
	@RequestMapping("details")
	public ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(categroyService.details(id));
		} catch (ProException e) {
			LOG.error("-系统-系统类型列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-系统类型列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改 **/
	@ApiVersion(1.0)
	@RequestMapping("update")
	public ResponseMsg update(@RequestBody ProjectCategory category) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			categroyService.update(category);
		} catch (ProException e) {
			LOG.error("-系统-系统类型列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-系统类型列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 系统类型添加 */
	@ApiVersion(1.0)
	@RequestMapping("add")
	public ResponseMsg add(@RequestBody ProjectCategory category) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			categroyService.addProjectCategroy(category);
		} catch (ProException e) {
			LOG.error("-系统-系统类型添加出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-系统类型添加异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 系统类型删除 */
	@ApiVersion(1.0)
	@RequestMapping("delete")
	public ResponseMsg delete(@RequestBody List<ProjectCategory> category) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			categroyService.deleteProjectCategroy(category);
		} catch (ProAffairException e) {
			LOG.error("-系统-系统类型删除出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-系统类型删除异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
